﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Services;
using Pg.BioMedics.SDR.Web.Controls;
using Pg.BioMedics.SDR.Web.Constants;

public partial class Administration_Views_OperatorsView : BaseHostedView
{
    #region Fields

    private IManagementService managementService;

    #endregion

    #region Properties

    /// <summary>
    /// Sets managment service instance. Utilized by IoC context for 
    /// managment service delivery to this view
    /// </summary>
    public IManagementService ManagmentService
    {
        set
        {
            this.managementService = value;
        }
    }

    #endregion

    #region Event handlers

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this.operatorsGridEX.DataSource = this.managementService.GetOperators();
            this.operatorsGridEX.DataBind();
        }
        catch (Exception ex)
        {
            base.Navigate(UsersManagementViews.ErrorView, ReturnViewName, ex);
        }
    }

    protected void ShowPersonDetails(object sender, EventArgs e)
    {
        base.Navigate(UsersManagementViews.PersonDetailsView, UsersManagementViews.OperatorsView, GetSelectedPerson());
    }

    #endregion

    #region Support methods

    private Person GetSelectedPerson()
    {
        int personId = (int)operatorsGridEX.GetRow().Cells["Id"].Value;
        return (from person in operatorsGridEX.DataSource as IList<Person>
                where (person.Id.HasValue) && (person.Id.Value == personId)
                select person).FirstOrDefault();
    }

    #endregion
}
